package cn.yunyichina.provider.hquery.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.hquery.thread.ExamineListAndDetailRunnable;
import cn.yunyichina.provider.hquery.vo.ExaminationVo;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineDetailVo;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineDetailVoResponse;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineListVo;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineListVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2016/12/12 0012.
 * 检验检查功能
 */
@Service("examineServiceImpl")
public class ExamineServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(ExamineServiceImpl.class);

    //编写Comparator,根据reportTime对ExamineListVoResponse进行排序
    private final Comparator<ExamineListVoResponse> COMPARATOR = new Comparator<ExamineListVoResponse>() {
       public int compare(ExamineListVoResponse o1, ExamineListVoResponse o2) {
           return o1.compareTo(o2);//运用User类的compareTo方法比较两个对象       
       }
    };
    
    /*
             检查结果列表和明细查询接口
     */
    public ResponseObject getExamineList(String data,String sessionId,String appId) {
    	
        logger.info("检查结果列表和明细 getExamineList data: "+data);
        ResponseObject response = new ResponseObject();
        ExaminationVo requestVo = JsonUtils.parseObject(data, ExaminationVo.class);
        
        if (StringUtils.isBlank(requestVo.getHospitalCode())
                ||StringUtils.isBlank(requestVo.getBeginDate())
                ||StringUtils.isBlank(requestVo.getEndDate())){
            response.setResultCodeAndMessage("18002","hospitalCode,beginDate,endDate不能为空");
            return response;
        }
        
        //结果集 检查列表信息 + 明细信息
        List<ExamineListVoResponse> examineListVoResponseList = null;
        
        //检查列表查询
        try {
            ExamineListVo examineListVo = new ExamineListVo();
            examineListVo.setHospitalCode(requestVo.getHospitalCode());
            examineListVo.setBranchCode(requestVo.getBranchCode());
            examineListVo.setPatCardType(requestVo.getPatCardType());
            examineListVo.setPatCardNo(requestVo.getPatCardNo());
            examineListVo.setBeginDate(requestVo.getBeginDate());
            examineListVo.setEndDate(requestVo.getEndDate());
            logger.info("调用调度中心开始");
            ResCommon<List<ExamineListVoResponse>> examineList  = HisifaceService.getHisifaceClient().getExamineList(examineListVo);
            logger.info("调用调度中心结束");
            if (!"0".equals(examineList.getResultCode())){
                logger.info("调用调度中心产生异常"+ examineList.getResultMessage());
                response.setResultCodeAndMessage("18998", examineList.getResultMessage());
                return response;
            }
            logger.info("调用调度中心返回 "+JsonUtils.toJsonString(examineList));
            
            examineListVoResponseList = examineList.getResult();
            
        } catch (Exception ex) {
            response.setResultCodeAndMessage("18999","系统异常");
            return  response;
        }

        //检查明细查询
        try {
            ExamineDetailVo examineDetailVo = new ExamineDetailVo();
            for(ExamineListVoResponse examineListVoResponse : examineListVoResponseList){
            	examineDetailVo.setHospitalCode(requestVo.getHospitalCode());
            	examineDetailVo.setBranchCode(requestVo.getBranchCode());
            	examineDetailVo.setCheckId(examineListVoResponse.getCheckId());
                examineDetailVo.setCheckType(examineListVoResponse.getCheckType());
                logger.info("调用调度中心开始");
                ResCommon<ExamineDetailVoResponse> examineDetail  = HisifaceService.getHisifaceClient().getExamineDetail(examineDetailVo);
                logger.info("调用调度中心结束");
                if (!"0".equals(examineDetail.getResultCode())){
                    logger.info("调用调度中心产生异常"+ examineDetail.getResultMessage());
                    response.setResultCodeAndMessage("18998", examineDetail.getResultMessage());
                    return response;
                }
                logger.info("调用调度中心返回 "+JsonUtils.toJsonString(examineDetail));
                examineListVoResponse.setExamineDetail(examineDetail.getResult());
            }

        } catch (Exception ex) {
            response.setResultCodeAndMessage("18999","系统异常");
            return  response;
        }
        
        List<ExamineListVoResponse> emptyReportimeList = new ArrayList<ExamineListVoResponse>();
        List<ExamineListVoResponse> notEmptyReportimeList = new ArrayList<ExamineListVoResponse>();

        for(ExamineListVoResponse u : examineListVoResponseList){
            if("".equals(u.getReportTime()) || u.getReportTime() == null){
                emptyReportimeList.add(u);
            } else {
                notEmptyReportimeList.add(u);
            }
        }
        //用我们写好的Comparator对notEmptyReportimeList进行reprotTime日期升序排序
        Collections.sort(notEmptyReportimeList, COMPARATOR);
        examineListVoResponseList.clear();
        examineListVoResponseList.addAll(notEmptyReportimeList);
        examineListVoResponseList.addAll(emptyReportimeList);
        
        if(CollectionUtils.isNotEmpty(examineListVoResponseList)){
        	//检查列表入库
            requestVo.setAppId(appId);
            logger.info("getExamineList request ={}",new Object[]{JsonUtils.toJsonString(requestVo)});
        	Thread  getExamineListAndDetailThread = new Thread(new ExamineListAndDetailRunnable(examineListVoResponseList,requestVo));
        	getExamineListAndDetailThread.start();
        }
        
        response.setResultCodeAndMessageAndResult("0","成功",examineListVoResponseList);
        logger.info("【检查结果列表和明细查询】结束, 返回结果=" + JsonUtils.toJsonString(response));
        return response;
    }
}
